package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoApiImpl implements VideoApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Override
    public String save(Video video) {
        //video补充完整
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        //2、调用方法保存对象
        mongoTemplate.save(video);
        //3、返回对象id
        return video.getId().toHexString();
    }

    @Override
    public List<Video> findMovementsByPids(List<Long> vids) {
        Query query = Query.query(Criteria.where("vid").is(vids));
        return mongoTemplate.find(query,Video.class);
    }

    @Override
    public List<Video> queryVideoList(int page, int pagesize) {
        Query query = new Query().skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query,Video.class);
    }

    @Override
    public Integer videoLike(ObjectId id, CommentType like) {
        //根据id查询
        Query query=Query.query(Criteria.where("id").is(id));
        Update update=new Update();
        update.inc("likeCount",1);
        //设置更新参数
        FindAndModifyOptions options=new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        Video video = mongoTemplate.findAndModify(query, update, options, Video.class);
        Integer likeCount = mongoTemplate.findOne(query, Video.class).getLikeCount();
        return likeCount;
    }

    @Override
    public Integer videoDisLike(ObjectId id, CommentType commentType) {
        //根据id查询
        Query query=Query.query(Criteria.where("id").is(id));
        Update update=new Update();
        update.inc("likeCount",-1);
        //设置更新参数
        FindAndModifyOptions options=new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        Video video = mongoTemplate.findAndModify(query, update, options, Video.class);
        Integer likeCount = mongoTemplate.findOne(query, Video.class).getLikeCount();
        return likeCount;
    }

    @Override
    public void videoLove(Long uid, CommentType love) {
        //根据id查询
        Query query=Query.query(Criteria.where("userId").is(uid));
        Update update=new Update();
        update.inc("loveCount",1);
        //设置更新参数
        FindAndModifyOptions options=new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        Video video = mongoTemplate.findAndModify(query, update, options, Video.class);
        //mongoTemplate.findOne(query, Video.class).getLikeCount();
    }

    @Override
    public void videoDisLove(Long uid, CommentType love) {
        //根据id查询
        Query query=Query.query(Criteria.where("userId").is(uid));
        Update update=new Update();
        update.inc("loveCount",-1);
        //设置更新参数
        FindAndModifyOptions options=new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        Video video = mongoTemplate.findAndModify(query, update, options, Video.class);
    }

    @Override
    public List<Video> selectVideos(Long uid, Integer page, Integer pagesize, String sortOrder, String sortProp) {
        Criteria criteria = Criteria.where("userId").is(uid);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize);
        if (sortOrder.equals("descending")) {
            query.with(Sort.by(Sort.Order.desc(sortProp)));
        } else {
            query.with(Sort.by(Sort.Order.asc(sortProp)));
        }
        List<Video> videos = mongoTemplate.find(query, Video.class);

        return videos;
    }
}
